package com.carlinfo.common.util;

import com.alibaba.fastjson.JSONObject;

public class PageInfoUtil
{
	/* 总条数 */
	private int totalRecord;
	/* 每页多少个 */
	private int pageSize = 10;
	/* 当前页 */
	private int currentPage;
	/* 总页数 */
	private int totalPage;
	/* 上一页 */
	private int prePage;
	/* 下一页 */
	private int nextPage;
	/* 每页的第一条记录的id */
	private int currentRecord;

	public int getTotalRecord()
	{
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord)
	{
		this.totalRecord = totalRecord;
	}

	public int getPageSize()
	{
		return pageSize;
	}

	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}

	/**
	 * currentPage:-1;3002328382383
	 * 
	 * @return
	 */
	public int getCurrentPage()
	{
		/* 当前页小于1 */
		if (this.currentPage < 1)
		{
			this.currentPage = 1;
		}
		/* 当前页大于总页数 */
		if (this.getTotalRecord() >= 0 && this.currentPage > this.getTotalPage())
		{
			this.currentPage = this.getTotalPage();
		} 
		if(this.currentPage == 0)
		{
			this.currentPage = 1;
		}
		return currentPage;
	}

	public void setCurrentPage(int currentPage)
	{
		this.currentPage = currentPage;
	}

	/**
	 * 总页数咋算出来的 总条数:30;每页:10条 总页数:30 / 10 == 3; 总条数:35;每页:10条 总页数:30 / 10 + 1== 3;
	 * 
	 * @return
	 */
	public int getTotalPage()
	{
		if (this.totalRecord % this.pageSize == 0)
		{
			this.totalPage = this.totalRecord / this.pageSize;
		} else
		{
			this.totalPage = this.totalRecord / this.pageSize + 1;
		}
		return totalPage;
	}

	public int getPrePage()
	{
		prePage = this.currentPage - 1;
		if (this.prePage < 1)
		{
			this.prePage = 1;
		}
		return prePage;
	}

	public int getNextPage()
	{
		this.nextPage = this.currentPage + 1;
		/* 当前页大于总页数 */
		if (this.nextPage > this.getTotalPage() && this.getTotalRecord()>=0)
		{
			this.nextPage = this.getTotalPage();
		}
		if (this.nextPage == 0)
		{
			this.nextPage = 1;
		}
		return nextPage;
	}

	/**
	 * 当前页的起始条数;每页10条 当前页 起始条数 1 1 2 11 3 21 4 31 5 51 起始条数如何算出来的:(当前页 - 1) * 每页多少条
	 * + 1 ;
	 * 
	 * @return
	 */
	public int getCurrentRecord()
	{
		/*这个按照道理来说最后是要加上1  但是因为sql语句分页的第一个参数时从0开始的 所以又把1去了*/
		this.currentRecord = (this.getCurrentPage() - 1) * 	this.getPageSize();
		return currentRecord;
	}
	public String toJSON()
	{
		JSONObject resultJSON = new JSONObject();
		resultJSON.put("currentPage", this.getCurrentPage() + "");
		resultJSON.put("pageSize", this.getPageSize() + "");
		resultJSON.put("totalRecord", this.getTotalRecord() + "");
		resultJSON.put("totalPage", this.getTotalPage() + "");
		resultJSON.put("prePage", this.getPrePage() + "");
		resultJSON.put("nextPage", this.getNextPage() + "");
		resultJSON.put("currentRecord", this.getCurrentRecord() + "");
		return resultJSON.toJSONString() ; 
	}
	public static void main(String[] args)
	{
		PageInfoUtil pageInfoUti = new PageInfoUtil();
		pageInfoUti.setTotalRecord(0);
		pageInfoUti.setCurrentPage(3);
		pageInfoUti.setPageSize(10);
		System.out.println("====>"+pageInfoUti.toJSON());
	}
}
